import axios from 'axios'
import {Message} from 'element-ui'
// create an axios instance
const instance = axios.create({
  //baseURL: 'http://192.168.3.51:8090/', // api的base_url
  //baseURL: 'http://11.73.23.91:8090/', // api的base_url
  baseURL: 'http://localhost:8090/', // api的base_url
  timeout: 50000 // request timeout
})

// request interceptor
instance.interceptors.request.use(
  config => {
	config.headers['Access-Control-Allow-Origin'] = "*"
	return config
  },
  error => {
    // Do something with request error
    // console.log(error) // for debug
    Promise.reject(error)
  }
)

// respone interceptor
instance.interceptors.response.use(
  response => response,
  error => {
    // console.log('err' + error) // for debug
    Message({
      message: error.message,
      type: 'error'
    })
    return Promise.reject(error)
  }
)

export const createAPI = (url, method, data) => {
  let config = {}
  if (method === 'get') {
    config.params = data
  } else {
    config.data = data
  }
  return instance({
    url,
    method,
    ...config
  })
}
